package countLatch;

import java.util.concurrent.Semaphore;

public class Semaphore2 {
    public static void main(String[] args) {
        //设定就有三个车位
        Semaphore semaphore = new Semaphore(3);
        //六个车去抢位置
        for (int i = 1; i < 7; i++) {
            new Thread(()->{
                //获取锁
                try {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName()+"抢到车位");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    semaphore.release();
                    System.out.println(Thread.currentThread().getName() + "离开车位");
                }
            },String.valueOf(i)).start();
        }
    }
}
